﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Pet
{
    public class Helper
    {
        /// <summary>
        /// 获取json配置
        /// </summary>
        /// <returns>返回菜单实体类</returns>
        public static MenuClass GetMenuSetting()
        {
            string path = "Setting.json";
            string line;
            string menuJson = "";
            MenuClass menuClass;
            if (!File.Exists(path))
            {
                //string[,] Arr = new string[,]{
                //    { "CMD控制台","cmd.exe"},
                //    { "开机自启动文件夹","shell:startup"},
                //    { "注册表编辑器","regedit"},
                //    { "远程桌面连接","mstsc"},
                //    { "CMD控制台","cmd"},
                //    { "CMD控制台","cmd"},
                //    { "CMD控制台","cmd"},
                //    { "CMD控制台","cmd"},
                //};
                List<string[]> Arr = new List<string[]>
                {
                    new string[]{ "CMD控制台","cmd.exe"},
                    new string[]{ "开机自启动文件夹","shell:startup"},
                    new string[]{ "注册表编辑器","regedit"},
                    new string[]{ "远程桌面连接","mstsc"},
                    new string[]{ "查询系统信息","cmd.exe /k systeminfo"},
                    new string[]{ "本地组策略编辑器","gpedit.msc"}
                };
                menuClass = new MenuClass { Menus = new List<Menu>() };
                foreach (var item in Arr)
                {
                    Menu menu = new Menu
                    {
                        text = item[0],
                        path = item[1],
                    };
                    menuClass.Menus.Add(menu);
                }
                menuJson = JsonConvert.SerializeObject(menuClass, Formatting.Indented);
                StreamWriter sw = new StreamWriter(path);
                sw.WriteLine(menuJson);
                sw.Close();
            }
            StreamReader sr = new StreamReader(path);
            menuJson = "";
            while ((line = sr.ReadLine()) != null)
            {
                menuJson += line + "\r\n";
            }
            menuClass = JsonConvert.DeserializeObject<MenuClass>(menuJson);
            return menuClass;
        }
        /// <summary>
        /// 运行
        /// </summary>
        /// <param name="cmd"></param>
        public static void StartProcess(string cmd)
        {
            int index = cmd.IndexOf(" ");
            if (index > 0)
            {
                Process.Start(cmd.Substring(0, index), cmd.Substring(index));
                return;
            }
            Process.Start(cmd);
        }
        public static string YiYan()
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://v1.hitokoto.cn?max_length=20");
                req.Method = "GET";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(res.GetResponseStream());
                var str = sr.ReadToEnd();
                JObject obj = JsonConvert.DeserializeObject<JObject>(str);
                res.Close();
                sr.Close();
                return obj["hitokoto"].ToString();
            }
            catch (Exception)
            {
                return "网络故障";
            }
        }
    }
}